<template>
  <div class="dept-move">
    <cl-form ref="Form" />
  </div>
</template>

<script lang="ts" name="dept-move" setup>
import { useCool } from "/@/cool";
import { ElMessage, ElMessageBox } from "element-plus";
import { useCrud, useForm } from "@cool-vue/crud";
import dayjs from "dayjs";

const { service } = useCool();

// cl-form
const Form = useForm();

// cl-crud
const Crud = useCrud();

function validDate(time: any) {
	const sDate = dayjs().subtract(1, "day").toDate();
	return sDate.getTime() > time.getTime();
}
// 打开
async function open(ids: any[], tip: any[]) {
	Form.value?.open({
		title: "减员",
		width: "400px",
		props: {
			labelWidth: "110px",
			labelPosition: "top"
		},
		items: [
			{
				label: "已选人员（" + tip.length + "）人",
				prop: "asslist",
				value: tip.join(","),
				component: {
					name: "el-input",
					props: {
						readonly: true,
						type: "textarea",
						rows: 2
					}
				}
			},
			{
				label: "选择减员日期",
				prop: "minusDate",
				value: dayjs(),
				component: {
					name: "el-date-picker",
					// props: {
					// 	disabledDate: validDate
					// }
				}
			}
		],
		on: {
			submit(data, { done, close }) {
				if (!data.minusDate) {
					ElMessage.warning("请选择减员日期");
					return done();
				}

				ElMessageBox.confirm("是否要减员？", "提示", {
					type: "warning"
				})
					.then(() => {
						service.demo.goods
							.minus({
								...data,
								ids: ids
							})
							.then(() => {
								ElMessage.success("减员成功");
								Crud.value?.refresh();
								close();
							})
							.catch((err) => {
								ElMessage.error(err.message);
								done();
							});
					})
					.catch(() => null);
			}
		}
	});
}

defineExpose({
	open
});
</script>
